<%@ Control Language="C#" AutoEventWireup="true" CodeFile="StateFlowTree.ascx.cs" Inherits="Admin_StateFlowTree" %>
<%@ Register Namespace="Data.DataSources" Assembly="App_Code" TagPrefix="DS"  %>
<%@ Register Src="~/Controls/StateFlow/StateFlow_Actions.ascx" TagPrefix="OR"  TagName="StateFlow_Actions" %>
<%@ Register Src="~/Controls/StateFlow/StateFlowActions.ascx" TagPrefix="OR"  TagName="StateFlowActions" %>
<%@ Register Src="~/Controls/StateFlow/States.ascx" TagPrefix="OR"  TagName="States" %>
<%@ Register Src="~/Controls/StateFlow/StateFlow_ActionsByPath.ascx" TagPrefix="OR"  TagName="StateFlow_ActionsByPath" %>
<script runat="server">
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<META name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<!-- #include virtual = "StateFlowTree.asp" -->
<?IMPORT NAMESPACE="MYTREE" IMPLEMENTATION="/include/HTC/TreeEx/treeex.htc" >
<?IMPORT NAMESPACE="WM"     IMPLEMENTATION="/Include/HTC/dropmenu.htc" >
<?IMPORT namespace="DIMOK" implementation="/include/htc/datagrid.htc">
<LINK rel="stylesheet" type="text/css" href="/CSS/Outlook.css">
<LINK rel="stylesheet" type="text/css" href="/CSS/navbar.css">
<SCRIPT src="/Include/common.js"></SCRIPT>
<STYLE>
.SF{FONT-SIZE: smaller}
</STYLE>
<script src="/Include/Tree.js"></script>
<script src="/Include/DropMenu.js"></script>
<!--
<BODY MS_POSITIONING="GridLayout" bgcolor="" onclick="showMenus()" onkeypress="if(event.keyCode==27)treeWorkFlow.selectedNodeIndex='100000'">
-->
<div style="overflow:auto">
<INPUT onchange="treeWorkFlow.showByAttrs(null,{'text':this.value})"> Find<BR>
<asp:UpdatePanel ID="upStateAndGroups" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:DropDownList id="selStateTables" DataSourceID="dsStateTables" AutoPostBack="true" DataTextField="TableName" DataValueField="TableName" runat="server" onchange="clearStateFromID();SF_Tree();"></asp:DropDownList> <span onclick="selStateTables().onchange()" style="cursor:hand">Tables</span>
<asp:DropDownList id="selGroups" DataSourceID="dsGroups" DataTextField="Name" DataValueField="principal_id" runat="server" onchange="clearStateFromID();SF_Tree();"></asp:DropDownList> Groups
</ContentTemplate>
</asp:UpdatePanel>
<MYTREE:TREEVIEWEX id="treeWorkFlow" SELECTEDNODEINDEX="100000" SYSTEMIMAGESPATH="/webctrl_client/1_0/TreeImages/" style="display:block"
	SHOWPLUS="true" EXPANDLEVEL="0" DEFAULTSTYLE="FONT-WEIGHT: bold; FONT-FAMILY: menu;"
	onnodebound="" onclick="setTimeout(function(){showActions()},100)" oncontextmenu="return showMenu(this)"
	NODESTATEATTRIBUTE="tabID" syncdatabind="XXX"
>
	<MYTREE:TREENODETYPE TYPE="N" DEFAULTSTYLE="COLOR:#003399" />
</MYTREE:TREEVIEWEX>
<DIV id="divMenus" style="position:absolute;top:0px;left:0px;display:none;width:1px;BORDER:black 1px solid;COLOR: white; BACKGROUND-COLOR: #001177;" align="center" nowrap>
<SPAN id="divMenuTitle" style="BORDER: black 1px solid; FONT-WEIGHT: bold"></SPAN><BR>
<asp:UpdatePanel ID="pnlStates" UpdateMode="Always" runat="server">
	<ContentTemplate>
		<asp:ListBox id="selStates" DataSourceID="dsStates" AutoPostBack="true" DataTextField="Name" DataValueField="ID" runat="server"
			Rows="8"
			onchange="if(!SF_Add(undefined,undefined,this))return;"
			onblur="showMenus(undefined,undefined,undefined,this)"
		></asp:ListBox>
	</ContentTemplate>
</asp:UpdatePanel>
</DIV>
<fieldset><legend state="+"><label>States -> Actions</label></legend>
<asp:UpdatePanel ID="pnlActions" UpdateMode="always" ChildrenAsTriggers="true" runat="server">
	<ContentTemplate>
		<OR:StateFlow_ActionsByPath ID="StateFlow_ActionsByPath" runat="server" />
	</ContentTemplate>
</asp:UpdatePanel>
</fieldset>
<asp:UpdatePanel ID="upStateFromID" runat="server">
<ContentTemplate>
		<asp:TextBox runat="server" id="hdnStateFromID" style="display:;position:absolute;left:0px;top:0px;height:0px;width:0px" enableviewstate="true" OnTextChanged="StateFromID_ValueChanged" AutoPostBack="true" />
</ContentTemplate>
</asp:UpdatePanel>
<fieldset><legend state="+"><label>States</label></legend>
<OR:States ID="States" runat="server" />
</fieldset>
<fieldset><legend state="+"><label>StateFlow Actions </label></legend>
<OR:StateFlowActions ID="StateFlowActions" runat="server" />
</fieldset>   
</div>
<DS:StateTablesDataSource  id="dsStateTables" runat="server">
	<SelectParameters>
		<asp:Parameter Name="ActiveOnly" Type="Boolean" DefaultValue="true" />
	</SelectParameters>
</DS:StateTablesDataSource>
<DS:GroupsDataSource ID="dsGroups" runat="server" />
<DS:StatesDataSource ID="dsStates" runat="server">
	<SelectParameters>
		 <asp:ControlParameter ControlID="selStateTables" Name="TableName" PropertyName="SelectedValue" Type="string" />
	</SelectParameters>
</DS:StatesDataSource>
<SCRIPT id="Menu">
pageLoads.push(loadTree);
function loadTree(){
	SF_Tree();
}

function Menu_List(dtg){
	var filter = (event.rs.filter||"").replace(/[*]/g,"%");
	var ret = WS(CO(),encodeXML(filter));
	if( showSOAPError(ret) )return false;
	dtg.setRecordset(ret.value);
}
function deleteRec(){
	if( !checkWS() || !confirm("Delete Record?") )event.returnValue = false;
	var ret = WS(CO("Menu_Delete"),_ID());
	if(showSOAPError(ret)) return event.returnValue = false;
	SF_List();
}
function editRec(){
	if(!checkWS()) event.returnValue = false;
	var ret = WS(CO("Menu_Update"),_ID(),event.fieldName,event.fieldValue);
	if(showSOAPError(ret)) return event.returnValue = false;
	SF_List();
}
function addRec(){
	if(!checkWS()) event.returnValue = false;
	var ret = WS(CO("Menu_Add"),getDSFromRS(event.rs,"xmlProgram"));
	if( showSOAPError(ret) )return event.returnValue = false;
	_ID(ret.value);
	SF_List();
}
function _ID(id){
	var idName = "MenuID";
	if( arguments.length )event.rs(idName).value = id;
	return event.rs(idName).value;
}
</SCRIPT>
<SCRIPT id="WorkFlow" language="javascript">
var u;
function hideSelects(show,selExlude){
	var sels = document.all.tags("SELECT");
	for( var i=0; i < sels.length; i++ )
		if( sels[i] != selExlude )sels[i].style.visibility = show ? 'visible' : 'hidden';
}
function showActions(){
	if( treeWorkFlow.selectedNodeIndex == showActions.selectedNodeIndex ){
		setTimeout("treeWorkFlow.selectedNodeIndex = showActions.selectedNodeIndex = '100000'",500);
		hdnStateFromID().value = "";
	}else{
		showActions.selectedNodeIndex = treeWorkFlow.selectedNodeIndex;
		var node = treeWorkFlow.getNodeFromIndex();
		if( !node )return;
		hdnStateFromID().value = node.id;
	}
	setTimeout("hdnStateFromID().onchange()",100);
	return;
	var ret = WS(CO("SF_Actions"),node.id);
	if( showSOAPError(ret) )return;
	xmlActions.loadXML(ret.value);
}
function UF_Action(el){
	var conf = el.tagName.match(/(INPUT|TEXTAREA)/) ? el.value : el[el.selectedIndex].text;
	if( !confirm("Change "+el.dataFld+" to:\n"+conf) ){
		if( el.tagName == "SELECT" )el.value = rsField(el,"MenuFromID").value;
		return false;
	}
	var ret = UF(u,u,u,u,rsField(el,"EventID").value||0);
	if( !ret || !ret.value )return;
	xmlActions.loadXML(ret.value);
}
function SF_Actions_Delete(el){
	if( !confirm("Delete ["+rsField(el,"MenuPathFrom")+"]->["+rsField(el,"MenuPathTo")+"]=["+rsField(el,"Action")+"]") )return;
	var ret = WS(CO(),rsField(el,"EventID").value);
	if( showSOAPError(ret) )return;
	xmlActions.recordset.Delete();
}
function SF_List(){
	var ret = WS(CO(),selNSs.value);
	if( showSOAPError(ret) )return;
	fillSelect(ret.value,selMenus,"MenuPath","MenuID");
}
function SF_Tree(){
	if( Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack() ) return;
  var tn = _tableName();
  if( !tn )return setTimeout("SF_Tree()",300);
	WSA.Admin.SF_Tree(_tableName(),selGroups().value,databind,wsError);
}
function SF_Add(fromID,fromText,sel){
	if( typeof(sel) == "undefined" ){
		SF_Add.fromID = fromID; SF_Add.fromText = fromText;
		var x = event.clientX;
		var y = event.clientY;
		return setTimeout(function(){showMenus(x,y,"... -> Path To",sel)},100);
	}
	//return confirm("Add path ["+SF_Add.fromText+"] => ["+sel[sel.selectedIndex].text+"]") );
	showMenus();
	if( !confirm("Add path ["+SF_Add.fromText+"] => ["+sel[sel.selectedIndex].text+"]") )return;
	WSA.Admin.SF_Add(SF_Add.fromID,sel.value,selGroups().value,databind,wsError);
	// Locals
}
function SFG_Add(parentID,kidID,parentText,kidText){
	if( !confirm("Grant access to path ["+parentText+"] => ["+kidText+"] to group ["+selGroups().value+"]?") )return;
	WSA.Admin.SFG_Add(parentID,kidID,selGroups().value,databind,wsError);
}
function SF_Rem(parentID,kidID,parentText,kidText){
	if( !confirm("Get rid of path ["+parentText+"] => ["+kidText+"]?") )return;
	var ret = WSA.Admin.SF_Rem(parentID,kidID,databind,wsError);
}
function SFG_Rem(parentID,kidID,parentText,kidText){
	if( !confirm("Revoke access to path ["+parentText+"] => ["+kidText+"] for group ["+selGroups().value+"]?") )return;
	WSA.Admin.SFG_Rem(parentID,kidID,selGroups().value,databind,wsError);
}
function wsError(result){
	//SF_Tree();
	showSOAPAlert(result.get_message());
}
function showMenu(el){
	var dm = new DropMenu("mnuFly");
	if( !dm.menuID )return false;
	if( event.treeNodeIndex === undefined )return;
	var node = el.getTreeNode(event.treeNodeIndex);
	var kidText = node.getNode().text;
	var kidID = node.getNode().id;
	var parentNode = node.getParent();
	if( parentNode ){
	
		var parentText = parentNode.getNode().text;
		var parentID = parentNode.getNode().id;
		dm.add("Add Access To Path","SFG_Add("+parentID+","+kidID+",\""+parentText+"\",\""+kidText+"\")","/Images/Tree_Little.gif");
		dm.add("Remove Access To Path","SFG_Rem("+parentID+","+kidID+",\""+parentText+"\",\""+kidText+"\")","/Images/KeyPress.gif");
		dm.add("Remove Path","SF_Rem("+parentID+","+kidID+",\""+parentText+"\",\""+kidText+"\")","/Images/KeyPress.gif");
	}
	dm.add("Add New Path","SF_Add("+kidID+",\""+kidText+"\")","/Images/Tree_Little.gif");
	dm.menuShow();
	event.cancelBubble = true;
	return false;
}
function showMenus(x,y,title,sel){
	sel = sel || selStates();
	if( !title ){
		hideSelects(true,sel);
		return divMenus.style.display = 'none';
	}
	divMenus.style.display = ""
	divMenus.style.posLeft = x;
	divMenus.style.posTop = y;
	divMenuTitle.innerHTML = title;
	sel.value = null;
	hideSelects(false,sel);
	sel.focus();
}
function databind(xml){
	if( !xml )return SF_Tree();
	treeWorkFlow.treeNodeSrc = transformTreeNodesToSrc(xml);
	treeWorkFlow.databind();
}
function _tableName(){
	var tn = selStateTables().value;
	if( !tn )return alert('No table name');
	return tn;
}
function clearStateFromID(){ hdnStateFromID().value=''; }
</SCRIPT>
